﻿using Red.Controls.Shell;
using Red.Controls.ViewModels;
using Red.EngineData.View;
using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace Red.Toolkit.WorldEditor
{
    /// <summary>
    ///     Interaction logic for SceneTree.xaml
    /// </summary>
    public partial class SceneTree : UserControl
    {
        public SceneTree()
        {
            InitializeComponent();
        }

        private void Load_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;
            sectorTreeItem.Execute( "load" );
        }

        private void Unload_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;
            sectorTreeItem.Execute( "unload" );
        }

        private void LoadAll_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;
            sectorTreeItem.Execute( "loadAll" );
        }

        private void UnloadAll_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;
            sectorTreeItem.Execute( "unloadAll" );
        }

        private async void AddSector_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;

            var sectorName = TextInputDialog.Show(this, "", "Sector name", "Create new sector");
            if (sectorName == null || sectorName.Length == 0)
            {
                return;
            }

            var res = await sectorTreeItem.ExecuteAsync( "addSector", sectorName );
            if ( !res )
                await RedMessageBox.ShowAsync( this, RedDialogType.Ok, res.Message );
        }

        private async void AddLayer_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;

            var layerName = TextInputDialog.Show(this, "", "Layer name", "Create new layer");
            if (layerName == null || layerName.Length == 0)
            {
                return;
            }

            var res = await sectorTreeItem.ExecuteAsync( "addLayer", layerName );
            if ( !res )
                await RedMessageBox.ShowAsync( this, RedDialogType.Ok, res.Message );
        }

        private async void Remove_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem treeItem = e.Parameter as InteropTreeItem;

            if ( !RedMessageBox.Show( this, RedDialogType.YesNo, String.Format( "Do you want to remove item {0}", treeItem.Title ) ) )
                return;

            var res = await treeItem.ExecuteAsync( "remove" );
            if ( !res )
                await RedMessageBox.ShowAsync( this, RedDialogType.Ok, res.Message );
        }

        private void ToggleVisibility_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            InteropTreeItem treeItem = e.Parameter as InteropTreeItem;
            treeItem.Execute( "toggleVisibility" );
        }

        private async void AddArea_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;

            /// TODO: area creator (indoor/outdoor, etc)
            /// 
            var areaName = TextInputDialog.Show(this, "", "Area name", "Create a new world area");
            if (areaName == null || areaName.Length == 0)
            {
                return;
            }

            var res = await sectorTreeItem.ExecuteAsync( "addArea", areaName );
            if ( !res )
                await RedMessageBox.ShowAsync( this, RedDialogType.Ok, res.Message );
        }

        private async void OpenArea_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;
            var res = await sectorTreeItem.ExecuteAsync( "open" );
            if ( !res )
                await RedMessageBox.ShowAsync( this, RedDialogType.Ok, res.Message );
        }

        private async void CloseArea_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            InteropTreeItem sectorTreeItem = e.Parameter as InteropTreeItem;
            var res = await sectorTreeItem.ExecuteAsync( "close" );
            if ( !res )
                await RedMessageBox.ShowAsync( this, RedDialogType.Ok, res.Message );
        }

        private async void OnActiveLayerChanged( DataItem item )
        {
            InteropTreeItem layerTreeItem = item as InteropTreeItem;

            var path = await layerTreeItem.Data.ExecuteAsync<string>( Interop.Make( "path" ) );

            InteropDataItem layersResolver = await ( DataContext as InteropDataItem ).DigUpSubItemAsync( "preview.layers" ) as InteropDataItem;
            layersResolver.Data.Execute( Interop.Make( "active = {0}", path ) );
        }
    }
}
